//
//  YLRefreshGifHeaderLottie.swift
//  JMIMSDK
//
//  Created by AlbertYuan on 2023/2/22.
/**
 使用demo：

 lazy var gifheader:YLRefreshGifHeaderLottie = {
     let gifh = YLRefreshGifHeaderLottie()
     gifh.refreshingBlock = {
         self.refreshAction()
     }
     return gifh
 }()

 tableView.mj_header = gifheader
 */

import UIKit
import Lottie
import MJRefresh
import SnapKit

public class YLRefreshGifHeaderLottie: MJRefreshGifHeader {

    public  var jsonString:String = ""
    public lazy var loadingView:LottieAnimationView = {
        let la = LottieAnimationView.init(name: self.jsonString)
        la.loopMode = .loop
        la.contentMode = .scaleAspectFit
        la.animationSpeed = 1
        return la
    }()

    // MARK: --  监听控件的刷新状态
    /**
     /** 普通闲置状态 */
     MJRefreshStateIdle = 1,
     /** 松开就可以进行刷新的状态 */
     MJRefreshStatePulling,
     /** 正在刷新中的状态 */
     MJRefreshStateRefreshing,
     /** 即将刷新的状态 */
     MJRefreshStateWillRefresh,
     /** 所有数据加载完毕，没有更多的数据了 */
     MJRefreshStateNoMoreData
     */
    public override var state: MJRefreshState{
        didSet{
            switch state {
            case .idle:
                self.stateLabel?.text = "下拉刷新"
                loadingView.stop()
            case .pulling:
                self.stateLabel?.text = "松开刷新"
                loadingView.play()
                break
            case .refreshing:
                self.stateLabel?.text = "正在刷新"
                loadingView.play()
            case .willRefresh:
                self.stateLabel?.text = "将要刷新"
                break
            case .noMoreData:
                self.stateLabel?.text = "没有更多"
                break
            @unknown default:
                break
            }
        }
    }

    public override func prepare() {
        super.prepare()
        self.lastUpdatedTimeLabel?.isHidden = true
        setJsonName(json: "loading.json")
    }
    
    public func setJsonName(json:String?){
        guard let json = json else{
            return
        }
        self.jsonString = json
        self.addSubview(loadingView)


        loadingView.snp.updateConstraints { make in
            make.centerY.equalToSuperview()
            make.width.height.equalTo(20)
            if let gifview = self.gifView{
                make.right.equalTo(gifview.snp.right).offset(10)
            }else{
                make.left.equalToSuperview().offset(45)
            }
        }
    }

    // MARK: --  - innerMethod
    public override func beginRefreshing(){
        UIImpactFeedbackGenerator().impactOccurred()
        self.layoutIfNeeded()
        super.beginRefreshing()
    }

    // MARK: -- 实时监听控件 scrollViewContentOffset
    public override func scrollViewContentOffsetDidChange(_ change: [AnyHashable : Any]?) {
        super.scrollViewContentOffsetDidChange(change)
        if self.jsonString != ""{
            if state.rawValue != 3 && state.rawValue != 2{
                loadingView.play(fromFrame: 0.0, toFrame: 1.0)
            }
        }
    }
}
